/**
 * 
 */
package GUI.main.navigation;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.AbstractListModel;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListModel;

import GUI.main.MainClient;
import GUI.main.PatientListRenderer;
import GUI.main.dialogs.AddUserDialog;

import officeserver.log_error.AppointmentException;
import officeserver.log_error.UserException;
import officeserver.users.PersonalInfo;
import officeserver.users.User;

import java.io.IOException;
import java.util.ArrayList;

/**
 * @author mramsey3
 * 
 */
public class AdminNavigationMenuPanel extends JPanel implements MouseListener,
	ActionListener {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    private JButton viewWaitingAppointments;
    private JPanel userPanel;
    private JPanel userTitlePanel;
    private JPanel userButtonPanel;
    private JButton createUser;
    private JButton deleteUser;
    private JScrollPane userScrollPane;
    private JList userList;

    /**
     * 
     */
    public AdminNavigationMenuPanel() {
	super();
	setLayout(new BorderLayout());
	add(getViewWaitingAppointments(), BorderLayout.SOUTH);
	add(getUserPanel(), BorderLayout.CENTER);
    }

    /**
     * @author mramsey3
     * @return
     */
    private JButton getViewWaitingAppointments() {
	if (viewWaitingAppointments == null) {
	    viewWaitingAppointments = new JButton("View Waiting Appointments");
	    viewWaitingAppointments.setPreferredSize(new Dimension(150, 25));
	    viewWaitingAppointments.setMargin(new Insets(0, 0, 0, 0));
	    viewWaitingAppointments.setFont(new Font(Font.SANS_SERIF,
		    Font.PLAIN, 12));
	    viewWaitingAppointments.putClientProperty("JButton.buttonType",
		    "textured");
	    viewWaitingAppointments.addMouseListener(this);
	}
	return viewWaitingAppointments;
    }

    /**
     * @author mramsey3
     * @return
     */
    private JPanel getUserPanel() {
	if (userPanel == null) {
	    userPanel = new JPanel();
	    userPanel.setPreferredSize(new Dimension(200, 450));
	    userPanel.setLayout(new BorderLayout());
	    userPanel.setBorder(BorderFactory.createBevelBorder(0));
	    userPanel.add(getUserTitlePanel(), BorderLayout.NORTH);
	    userPanel.add(getUserScrollPane(), BorderLayout.CENTER);
	    userPanel.add(getUserButtonPanel(), BorderLayout.SOUTH);
	    userPanel.addMouseListener(this);
	}
	return userPanel;
    }

    /**
     * @author mramsey3
     * @return
     */
    private JPanel getUserTitlePanel() {
	userTitlePanel = new JPanel() {
	    private static final long serialVersionUID = 1L;

	    /**
	     * custom paintCompontent for friendTitlePanel.
	     * 
	     * @param g
	     *            system Graphics
	     */
	    public void paintComponent(Graphics g) {
		super.paintComponent(g);
		g.setColor(new Color(100, 150, 220));
		g.fillRect(0, 0, getWidth(), getHeight());
		g.setColor(Color.white);
		g.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 12));
		g.drawString("PATIENTS:", 10, getHeight() - 8);
	    }
	};
	userTitlePanel.setPreferredSize(new Dimension(200, 25));
	userTitlePanel.addMouseListener(this);

	return userTitlePanel;
    }

    /**
     * @author mramsey3
     * @return
     */
    private JPanel getUserButtonPanel() {
	if (userButtonPanel == null) {
	    userButtonPanel = new JPanel();
	    userButtonPanel.setPreferredSize(new Dimension(200, 30));
	    userButtonPanel.setLayout(new GridBagLayout());
	    GridBagConstraints gbc1 = new GridBagConstraints();
	    gbc1.gridx = 0;
	    userButtonPanel.add(getCreateUser(), gbc1);

	    GridBagConstraints gbc2 = new GridBagConstraints();
	    gbc2.gridx = 1;
	    userButtonPanel.add(Box.createHorizontalStrut(2), gbc2);

	    GridBagConstraints gbc3 = new GridBagConstraints();
	    gbc3.gridx = 2;
	    userButtonPanel.add(getDeleteUser(), gbc3);
	    userButtonPanel.addMouseListener(this);
	}
	return userButtonPanel;
    }

    /**
     * @author mramsey3
     * @return
     */
    private JButton getCreateUser() {
	if (createUser == null) {
	    createUser = new JButton("Create");
	    createUser.setPreferredSize(new Dimension(75, 25));
	    createUser.setMargin(new Insets(0, 0, 0, 0));
	    createUser.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 12));
	    createUser.putClientProperty("JButton.buttonType", "textured");
	    createUser.addActionListener(this);
	}
	return createUser;
    }

    /**
     * @author mramsey3
     * @return
     */
    private JButton getDeleteUser() {
	if (deleteUser == null) {
	    deleteUser = new JButton("Delete");
	    deleteUser.setPreferredSize(new Dimension(75, 25));
	    deleteUser.setMargin(new Insets(0, 0, 0, 0));
	    deleteUser.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 12));
	    deleteUser.putClientProperty("JButton.buttonType", "textured");
	    deleteUser.addActionListener(this);
	}
	return deleteUser;
    }

    /**
     * @author mramsey3
     * @return
     */
    private JScrollPane getUserScrollPane() {
	if (userScrollPane == null) {
	    userScrollPane = new JScrollPane();
	    userScrollPane.setPreferredSize(new Dimension(200, 0));
	    userScrollPane.setViewportView(getUserList());
	}
	return userScrollPane;
    }

    /**
     * @author mramsey3
     * @return
     */
    private JList getUserList() {
	if (userList == null) {
	    userList = new JList(new PatientListModel());
	    userList.setCellRenderer(new PatientListRenderer());
	    userList.addMouseListener(new DoubleClickListener(userList));
	}
	return userList;
    }

    /**
     * @author mramsey3
     * @param p
     */
    public void refreshPanel() {
	userList.updateUI();
	repaint();
    }

    private void loadUserDialog(User user) {

    }

    /**
     * @author mramsey3
     * 
     */
    private class PatientListModel extends AbstractListModel {
	private static final long serialVersionUID = 1L;

	public Object getElementAt(int i) {
	    ArrayList<PersonalInfo> users = null;
	    try {
		users = MainClient.getConnection().getAllUsers();
	    } catch (UserException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	    } catch (AppointmentException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	    } catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	    } catch (ClassNotFoundException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	    }
	    return users.get(i);
	}

	public int getSize() {
	    ArrayList<PersonalInfo> users = null;
	    try {
		users = MainClient.getConnection().getAllUsers();
	    } catch (UserException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	    } catch (AppointmentException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	    } catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	    } catch (ClassNotFoundException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	    }
	    return users.size();
	}
    }

    /**
     * @author mramsey3
     * 
     */
    private class DoubleClickListener extends MouseAdapter {
	protected JList list;

	/**
	 * @param l
	 */
	public DoubleClickListener(JList l) {
	    list = l;
	}

	public void mouseClicked(MouseEvent e) {
	    if (e.getSource() == userList)
		if (e.getClickCount() == 2) {
		    if (userList.getModel().getSize() > 0) {
			int index = list.locationToIndex(e.getPoint());
			ListModel lm = list.getModel();
			User u = (User) (lm.getElementAt(index));
			refreshPanel();
			loadUserDialog(u);
		    }
		}
	}
    }

    @Override
    public void actionPerformed(ActionEvent e) {
	if (e.getSource() == createUser) {
	    AddUserDialog aud = new AddUserDialog(this);
	    aud.setVisible(true);
	}
	if(e.getSource() == deleteUser){
	    switch(JOptionPane.showConfirmDialog(this, "Are you sure you want to delete user?")){
	    case JOptionPane.YES_OPTION:
		try {
		    MainClient.getConnection().userSearch((((PersonalInfo)userList.getSelectedValue()).getUsername()));
		    MainClient.getConnection().deleteSearchUser();
		    refreshPanel();
		} catch (UserException e1) {
		    e1.printStackTrace();
		} catch (IOException e1) {
		    e1.printStackTrace();
		} catch (ClassNotFoundException e1) {
		    e1.printStackTrace();
		}
		break;
	    case JOptionPane.NO_OPTION:
		break;
	    case JOptionPane.CANCEL_OPTION:
		break;
	    default:
		
	    }
	}

    }

    public void mouseClicked(MouseEvent e) {
    }

    public void mouseEntered(MouseEvent e) {
    }

    public void mouseExited(MouseEvent e) {
    }

    public void mousePressed(MouseEvent e) {
    }

    public void mouseReleased(MouseEvent e) {
    }
}
